﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommonLib;
using System.Reflection;
using System.Data;
using System.Web;
using Entities;
using Entities.System;

namespace Framework
{
    public class BusinessBase
    {
        protected static string getCondition(Dictionary<string, string> dicLike, Dictionary<string, string> dicEqual)
        {
            string conditon = string.Empty;
            foreach (var item in dicLike)
            {
                if (!string.IsNullOrEmpty(item.Value)) conditon += string.Format(" and {0} like '%{1}%' ", item.Key, item.Value);
            }
            foreach (var item in dicEqual)
            {
                if (!string.IsNullOrEmpty(item.Value)) conditon += string.Format(" and {0} = '{1}' ", item.Key, item.Value);
            }
            return conditon;
        }

        protected static string getCondition(Dictionary<string, string> dicLike, Dictionary<string, string> dicEqual, 
            Dictionary<string, string> dicIn)
        {
            string conditon = string.Empty;
            foreach (var item in dicLike)
            {
                if (!string.IsNullOrEmpty(item.Value)) conditon += string.Format(" and {0} like '%{1}%' ", item.Key, item.Value);
            }
            foreach (var item in dicEqual)
            {
                if (!string.IsNullOrEmpty(item.Value)) conditon += string.Format(" and {0} = '{1}' ", item.Key, item.Value);
            }
            foreach (var item in dicIn)
            {
                if (!string.IsNullOrEmpty(item.Value)) conditon += string.Format(" and {0} in ({1}) ", item.Key, item.Value);
            }
            return conditon;
        }

        protected static string getOrCondition(Dictionary<string, string> dicLike, Dictionary<string, string> dicEqual)
        {
            string conditon = string.Empty;
            if (dicLike.Count > 0 || dicEqual.Count > 0)
            {
                conditon += " and  (";
                foreach (var item in dicLike)
                {
                    if (!string.IsNullOrEmpty(item.Value)) conditon += string.Format(" ({0} like '%{1}%') or ", item.Key, item.Value);
                }

                foreach (var item in dicEqual)
                {
                    if (!string.IsNullOrEmpty(item.Value)) conditon += string.Format("  ({0} = '{1}') or ", item.Key, item.Value);
                }
                conditon = conditon.Substring(0, conditon.Length - 3);
                conditon += ")";

            }
            return conditon;
        }

        public string QuoteString(string value)
        {
            value = value.Replace("'", "''");
            return value = "'" + value + "'";
        }

        public bool isAdmin()
        {
            //判断是否超级管理员登录
            User user = HttpContext.Current.Session["user"] as User;
            return user.USER_ID == "Administrator";
        }

        public static string getJson<T>(T t) where T : class
        {
            return t.ToJson();
        }

        /// <summary>
        /// 获得实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dt"></param>
        /// <returns></returns>
        public List<T> GetEntityList<T>(DataTable dt) where T : class
        {

            List<T> tlist = new List<T>();

            int rows = dt.Rows.Count;

            PropertyInfo[] tpis = typeof(T).GetProperties();

            foreach (DataRow item in dt.Rows)
            {

                T t = null;

                t = (T)Activator.CreateInstance(typeof(T));

                foreach (PropertyInfo p in tpis)
                {

                    if (dt.Columns.Contains(p.Name) && item[p.Name] != DBNull.Value)
                    {

                        p.SetValue(t, item[p.Name], null);

                    }

                }
                tlist.Add(t);


            }
            return tlist;
        }
    }

}